﻿using APP.HELPER.Utils.Security;
using Microsoft.VisualBasic;
using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Transactions;

namespace APP.HELPER.Data.Helper
{
    public class SqlHelper
    {
        public delegate void ConnectedEventHander(object sender, SqlConnection e);
        public delegate void ConnectEventHander(object sender);
        public delegate void ErrorEventHander(object sender, SqlHelperException e);
        public delegate void Execute(object sender, int Percent);
        public delegate void Executed(object sender);
        public delegate void ExecuteStart(object sender);
        public delegate void TransactionClosedEventHander(object sender);
        public delegate void TransactionEventHander(object sender);
        private static bool _HideError = false;
        private static string _GlobalConnectString = "";
        private static string _serverConnectionString = "";
        private static int _count = 0;
        private bool _autoCommand = true;
        private bool _authentication = true;
        private SqlConnection _connection;
        private string _connectionString = "";
        private string _database = "";
        private string _password = "";
        private string _server = "";
        private SqlTransaction _transaction;
        private string _userid = "";
        private string _result = "";
        private string _commandText = "";
        private bool _mustCloseConnection;
        private SqlHelperException _sqlHelperException;
        private bool _isFormatMessage = true;
        private CommandType _commandType = CommandType.StoredProcedure;
        private int m_rowaffected;
        private bool m_useTransaction;
        private bool _isextract;
        public event SqlHelper.TransactionClosedEventHander TransactionClosed;
        public event TransactionStartedEventHandler TransactionStarted;
        public event TransactionCompletedEventHandler TransactionCompleted;
        public event SqlHelper.TransactionEventHander TransactionStart;
        public event SqlHelper.ConnectEventHander Connect;
        public event SqlHelper.ConnectedEventHander Connected;
        public event SqlHelper.ErrorEventHander Error;
        public static string ServerConnectString
        {
            get
            {
                if (!(SqlHelper._serverConnectionString == ""))
                {
                    return SqlHelper._serverConnectionString;
                }
                string item;
                if (!SqlHelper.IsAuthe(SqlHelper._GlobalConnectString))
                {
                    item = SqlHelper.GetItem("server=", SqlHelper._GlobalConnectString);
                    string item2 = SqlHelper.GetItem("user id=", SqlHelper._GlobalConnectString);
                    string item3 = SqlHelper.GetItem("password=", SqlHelper._GlobalConnectString);
                    return string.Concat(new string[]
					{
						"server=",
						item,
						";user id= ",
						item2,
						" ;password=",
						item3,
						";"
					});
                }
                item = SqlHelper.GetItem("data source=", SqlHelper._GlobalConnectString);
                return "Data Source=" + item + ";Initial Catalog=master;Integrated Security=True;";
            }
            set
            {
                SqlHelper._serverConnectionString = value;
            }
        }
        public static string ConnectString
        {
            get
            {
                return SqlHelper._GlobalConnectString;
            }
            set
            {
                SqlHelper._GlobalConnectString = value;
            }
        }
        public string ConnectionString
        {
            get
            {
                return this._connectionString;
            }
            set
            {
                this._connectionString = value;
            }
        }
        public SqlConnection Connection
        {
            get
            {
                return this._connection;
            }
        }
        public SqlTransaction Transaction
        {
            get
            {
                return this._transaction;
            }
        }
        public string Database
        {
            get
            {
                return this._database;
            }
            set
            {
                this._database = value;
            }
        }
        public string Server
        {
            get
            {
                return this._server;
            }
            set
            {
                this._server = value;
            }
        }
        public string UserID
        {
            get
            {
                return this._userid;
            }
            set
            {
                this._userid = value;
            }
        }
        public string Password
        {
            get
            {
                return this._password;
            }
            set
            {
                this._password = value;
            }
        }
        public bool Authentication
        {
            get
            {
                return this._authentication;
            }
            set
            {
                this._authentication = value;
            }
        }
        public static bool HideError
        {
            get
            {
                return SqlHelper._HideError;
            }
            set
            {
                SqlHelper._HideError = value;
            }
        }
        public bool IsExtract
        {
            get
            {
                return this._isextract;
            }
            set
            {
                this._isextract = value;
                if (this._isextract)
                {
                    this.Extract();
                }
            }
        }
        public string Result
        {
            get
            {
                return this._result;
            }
            set
            {
                this._result = value;
            }
        }
        public string CommandText
        {
            get
            {
                return this._commandText;
            }
            set
            {
                this._commandText = value;
            }
        }
        public bool MustCloseConnection
        {
            get
            {
                return this._mustCloseConnection;
            }
            set
            {
                this._mustCloseConnection = value;
            }
        }
        public SqlHelperException SqlHelperException
        {
            get
            {
                return this._sqlHelperException;
            }
        }
        public bool IsConnection
        {
            get
            {
                return this._connection.State == ConnectionState.Open;
            }
        }
        public bool IsFormatMessage
        {
            get
            {
                return this._isFormatMessage;
            }
            set
            {
                this._isFormatMessage = value;
            }
        }
        public static int Count
        {
            get
            {
                return SqlHelper._count;
            }
            set
            {
                SqlHelper._count = value;
            }
        }
        public CommandType CommandType
        {
            get
            {
                return this._commandType;
            }
            set
            {
                this._commandType = value;
            }
        }
        public bool AutoCommand
        {
            get
            {
                return this._autoCommand;
            }
            set
            {
                this._autoCommand = value;
            }
        }
        public int Rowaffected
        {
            get
            {
                return this.m_rowaffected;
            }
            set
            {
                this.m_rowaffected = value;
            }
        }
        public bool UseTransaction
        {
            get
            {
                return this.m_useTransaction;
            }
            set
            {
                this.m_useTransaction = value;
            }
        }
        public static void LoadConnectString()
        {
            if (ConfigurationSettings.AppSettings != null)
            {
                if (ConfigurationSettings.AppSettings["connectionString"] == null)
                {
                    return;
                }
                SqlHelper._GlobalConnectString = ConfigurationSettings.AppSettings["connectionString"].ToString();
            }
        }
        private void RaiseTransactionClosedEventHander()
        {
            if (this.TransactionClosed != null)
            {
                this.TransactionClosed(this);
            }
        }
        private void RaiseTransactionCompletedEventHander(TransactionEventArgs e)
        {
            if (this.TransactionCompleted != null)
            {
                this.TransactionCompleted(this, e);
            }
        }
        private void RaiseTransactionStartedEventHander(TransactionEventArgs e)
        {
            if (this.TransactionStarted != null)
            {
                this.TransactionStarted(this, e);
            }
        }
        private void RaiseTransactionEventHander()
        {
            if (this.TransactionStart != null)
            {
                this.TransactionStart(this);
            }
        }
        private void RaiseErrorEventHander(SqlHelperException e)
        {
            if (this.Error != null)
            {
                this.Error(this, e);
            }
        }
        private void RaiseConnectEventHander()
        {
            if (this.Connect != null)
            {
                this.Connect(this);
            }
        }
        private void RaiseConnectedEventHander(SqlConnection e)
        {
            if (this.Connected != null)
            {
                this.Connected(this, e);
            }
        }
        public void Extract()
        {
            this._authentication = SqlHelper.IsAuthe(this._connectionString);
            if (!this._authentication)
            {
                this._server = SqlHelper.GetItem("server=", this._connectionString);
                this._database = SqlHelper.GetItem("database=", this._connectionString);
                this._userid = SqlHelper.GetItem("user id=", this._connectionString);
                this._password = SqlHelper.GetItem("password=", this._connectionString);
                return;
            }
            this._server = SqlHelper.GetItem("data source=", this._connectionString);
            this._database = SqlHelper.GetItem("initial catalog=", this._connectionString);
        }
        public SqlHelper()
        {
            this._connectionString = SqlHelper._GlobalConnectString;
            this._connection = new SqlConnection();
            SqlHelper.Count++;
        }
        public SqlHelper(string serverName, string dataBaseName, string userName, string passWord)
        {
            this._server = serverName;
            this._database = dataBaseName;
            this._userid = userName;
            this._password = passWord;
            this._authentication = false;
            this._connectionString = this.RebuildConnectionString();
            this._connection = new SqlConnection();
            SqlHelper.Count++;
        }
        public SqlHelper(string serverName, string dataBaseName, string userName, string passWord, bool authen)
        {
            this._server = serverName;
            this._database = dataBaseName;
            this._userid = userName;
            this._password = passWord;
            this._authentication = authen;
            this._connectionString = this.RebuildConnectionString();
            this._connection = new SqlConnection();
            SqlHelper.Count++;
        }
        public SqlHelper(string serverName, string dataBaseName)
        {
            this._server = serverName;
            this._database = dataBaseName;
            this._userid = "";
            this._password = "";
            this._authentication = true;
            this._connectionString = this.RebuildConnectionString(this._server, this._database);
            this._connection = new SqlConnection();
            SqlHelper.Count++;
        }
        public SqlHelper(string serverName, int authencation)
        {
            this._server = serverName;
            this._database = "";
            this._userid = "";
            this._password = "";
            this._authentication = true;
            this._connectionString = this.RebuildConnectionString(this._server);
            this._connection = new SqlConnection();
            SqlHelper.Count++;
        }
        public SqlHelper(string connectstring, string password, bool isCrypt)
        {
            if (isCrypt)
            {
                connectstring = MyEncryption.Decrypt(connectstring, password, true);
            }
            if (SqlHelper.ConnectString == null)
            {
                throw new ArgumentNullException("connectstring");
            }
            if (string.IsNullOrEmpty(connectstring))
            {
                throw new SqlHelperException("connectstring");
            }
            this._connectionString = connectstring;
            this._connection = new SqlConnection();
            SqlHelper.Count++;
        }
        public string Encrypt()
        {
            return this.Encrypt(this._connectionString, "!@#$%^&*()Pofd154$#@");
        }
        public string Encrypt(string connectstring, string password)
        {
            this._connectionString = connectstring;
            if (this._connection == null)
            {
                this._connection = new SqlConnection();
            }
            return MyEncryption.Encrypt(connectstring, password, true);
        }
        public string Decrypt()
        {
            return this.Decrypt(this._connectionString, "!@#$%^&*()Pofd154$#@");
        }
        public string Decrypt(string connectstring, string password)
        {
            connectstring = MyEncryption.Decrypt(connectstring, password, true);
            this._connectionString = connectstring;
            if (this._connection == null)
            {
                this._connection = new SqlConnection();
            }
            return this._connectionString;
        }
        public SqlHelper(string connectString)
        {
            if (string.IsNullOrEmpty(connectString))
            {
                throw new SqlHelperException("connectString");
            }
            this._connectionString = connectString;
            this._connection = new SqlConnection();
            SqlHelper.Count++;
        }
        public SqlHelper(SqlConnection connection)
        {
            if (connection == null)
            {
                throw new SqlHelperException("connection");
            }
            this._connection = connection;
            this._connectionString = this.Connection.ConnectionString;
            SqlHelper.Count++;
        }
        private static string GetItem(string getStr, string data)
        {
            if (string.IsNullOrEmpty(data))
            {
                string message = SqlHelper._HideError ? "Chưa cấu hình chuỗi kết nối." : "Connecttion String is fail.";
                throw new SqlHelperException(message);
            }
            string text = data.ToLower();
            int num = text.IndexOf(getStr);
            if (num == -1)
            {
                return string.Empty;
            }
            num += getStr.Length;
            int length = text.IndexOf(';', num) - num;
            return data.Substring(num, length).Trim();
        }
        private static bool IsAuthe(string data)
        {
            if (string.IsNullOrEmpty(data))
            {
                string message;
                if (SqlHelper._HideError)
                {
                    message = "Chưa cấu hình chuỗi kết nối.";
                }
                else
                {
                    message = "Connecttion String is fail.";
                }
                throw new SqlHelperException(message);
            }
            return data.ToLower().IndexOf("data source=") != -1;
        }
        public string Open()
        {
            this.RaiseConnectEventHander();
            if (string.IsNullOrEmpty(this._connectionString))
            {
                throw new SqlHelperException("connectionString");
            }
            if (this._connection == null)
            {
                this._connection = new SqlConnection(this._connectionString);
            }
            return this.Open(this._connection);
        }
        public string Open(SqlConnection myConnection)
        {
            if (myConnection == null)
            {
                throw new SqlHelperException("myConnection");
            }
            this.RaiseConnectEventHander();
            if (myConnection.State == ConnectionState.Open)
            {
                return this.Result;
            }
            try
            {
                if (string.IsNullOrEmpty(this._connectionString))
                {
                    throw new SqlHelperException("_connectionString");
                }
                myConnection.ConnectionString = this._connectionString;
                myConnection.Open();
                this.RaiseConnectedEventHander(myConnection);
                this.Result = "OK";
                return this.Result;
            }
            catch (SqlException ex)
            {
                if (SqlHelper._HideError)
                {
                    this.Result = "Không thể kết nối với máy chủ.\nChi tiết:\n\t" + ex.Message;
                    this.Result = this.DispError(ex.Number, this.Result, "");
                }
                else
                {
                    this.Result = ex.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result, ex.Number));
            }
            catch (Exception ex2)
            {
                if (SqlHelper._HideError)
                {
                    this.Result = "Không thể kết nối với máy chủ.\nChi tiết:\n\t" + ex2.Message;
                    this.Result = this.DispError(-1, this.Result, "");
                }
                else
                {
                    this.Result = ex2.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result));
            }
            return this.Result;
        }
        public override string ToString()
        {
            return this._connectionString;
        }
        public void Close()
        {
            this.Close(this._connection);
        }
        public void Close(SqlConnection myConnection)
        {
            if (myConnection == null)
            {
                return;
            }
            if (myConnection.State == ConnectionState.Open)
            {
                try
                {
                    myConnection.Close();
                }
                catch (SqlException)
                {
                    this.Result = "Có lỗi xảy ra khi mở kết nối đến máy chủ.";
                }
                catch (Exception)
                {
                }
            }
            this._connection.Dispose();
            this._connection = null;
        }
        ~SqlHelper()
        {
            SqlHelper.Count--;
            this.Close();
            if (this._transaction != null && this._transaction.Connection != null)
            {
                try
                {
                    if (this._transaction.Connection.State == ConnectionState.Open)
                    {
                        this._transaction.Connection.Close();
                    }
                    this._transaction.Rollback();
                    this._transaction = null;
                }
                catch (Exception)
                {
                }
            }
        }
        public string Start()
        {
            this.Result = this.Open();
            if (this.Result != "OK")
            {
                this.RaiseErrorEventHander(new SqlHelperException(this.Result));
                return this.Result;
            }
            try
            {
                this.RaiseTransactionEventHander();
                this._transaction = this._connection.BeginTransaction();
                TransactionEventArgs e = new TransactionEventArgs();
                this.RaiseTransactionStartedEventHander(e);
                this.Result = "OK";
                return this.Result;
            }
            catch (SqlException ex)
            {
                if (SqlHelper._HideError)
                {
                    this.Result = this.DispError(ex.Number, "Không thể khởi tạo giao dịch.\nChi tiết:\n\t" + ex.Message, "");
                }
                else
                {
                    this.Result = ex.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result, ex.Number));
            }
            catch (Exception ex2)
            {
                if (SqlHelper._HideError)
                {
                    this.Result = "Không thể khởi tạo giao dịch.\nChi tiết:\n\t" + ex2.Message;
                }
                else
                {
                    this.Result = ex2.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result));
            }
            return this.Result;
        }
        public string Commit()
        {
            return this.Commit(this._transaction);
        }
        public string Commit(SqlTransaction myTransaction)
        {
            if (myTransaction == null)
            {
                throw new SqlHelperException("myTransaction");
            }
            this.Result = "";
            try
            {
                myTransaction.Commit();
                this.RaiseTransactionClosedEventHander();
                this.Close();
                this.Result = "OK";
                return this.Result;
            }
            catch (InvalidOperationException ex)
            {
                if (SqlHelper._HideError)
                {
                    this.Result = "Các giao dịch đã được hoàn thành hoặc được khôi phục lại.\nOr kết nối bị huỹ.";
                }
                else
                {
                    this.Result = ex.Message;
                }
            }
            catch (SqlException ex2)
            {
                if (SqlHelper._HideError)
                {
                    this.Result = "Không thể kết thúc giao dịch.\nChi tiết:\n\t" + ex2.Message;
                }
                else
                {
                    this.Result = ex2.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result, ex2.Number));
            }
            catch (Exception ex3)
            {
                if (SqlHelper._HideError)
                {
                    this.Result = "Có lỗi xảy ra trong khi cố gắng hoàn thành giao dịch.";
                }
                else
                {
                    this.Result = ex3.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result));
            }
            return this.Result;
        }
        public string RollBack()
        {
            return this.RollBack(this._transaction);
        }
        public string RollBack(SqlTransaction myTransaction)
        {
            this.Result = "";
            if (this._transaction == null)
            {
                if (SqlHelper._HideError)
                {
                    this.Result = "Giao dịch chưa được khởi tạo.";
                }
                else
                {
                    this.Result = "Don't has Transaction created.";
                }
                this.Close();
                return this.Result;
            }
            try
            {
                if (myTransaction.Connection != null)
                {
                    myTransaction.Rollback();
                }
                myTransaction.Dispose();
                myTransaction = null;
                this.Result = "OK";
                return this.Result;
            }
            catch (SqlException ex)
            {
                if (SqlHelper._HideError)
                {
                    this.Result = "Không thể khôi phục giao dịch.\nChi tiết:\n\t" + ex.Message;
                }
                else
                {
                    this.Result = ex.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result, ex.Number));
            }
            catch (Exception ex2)
            {
                if (SqlHelper._HideError)
                {
                    this.Result = "Không thể khôi phục giao dịch.\nChi tiết:\n\t" + ex2.Message;
                }
                else
                {
                    this.Result = ex2.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result));
            }
            finally
            {
                this.Close();
            }
            return this.Result;
        }
        public DataTable ExecuteDataTable(string commandText, string[] myParams, object[] myValues)
        {
            DataTable result = null;
            if (this.m_useTransaction)
            {
                this.Result = this.Start();
                if (this.Result != "OK")
                {
                    this.RollBack();
                    return result;
                }
                result = this.ExecuteDataTable(this._transaction, commandText, myParams, myValues);
                if (this.Result == "OK")
                {
                    this.Commit();
                }
            }
            else
            {
                if (this.Open() == "OK")
                {
                    result = this.ExecuteDataTable(this._connection, commandText, myParams, myValues);
                }
                this.Close();
            }
            return result;
        }
        public DataTable ExecuteDataTable(string commandText)
        {
            return this.ExecuteDataTable(commandText, null, null);
        }
        public DataTable ExecuteDataTable(SqlConnection myConnection, string commandText, string[] myParams, object[] myValues)
        {
            return this.ExecuteDataTable(myConnection, this.CommandType, commandText, myParams, myValues);
        }
        public DataTable ExecuteDataTable(SqlConnection myConnection, CommandType commandType, string commandText, string[] myParams, object[] myValues)
        {
            if (myConnection == null)
            {
                throw new SqlHelperException("myConnection");
            }
            if (myConnection.State != ConnectionState.Open)
            {
                throw new SqlHelperException("Connnection is close");
            }
            if (string.IsNullOrEmpty(commandText))
            {
                throw new SqlHelperException("commandText");
            }
            bool flag = false;
            if (myParams != null & myValues != null)
            {
                flag = true;
                if (myParams.Length != myValues.Length)
                {
                    throw new SqlHelperException("Parameter count does not match Parameter Value count.");
                }
            }
            this.Result = "";
            DataTable dataTable = new DataTable();
            SqlCommand sqlCommand = myConnection.CreateCommand();
            sqlCommand.CommandText = commandText;
            sqlCommand.Connection = myConnection;
            sqlCommand.CommandType = commandType;
            if (flag)
            {
                for (int i = 0; i < myParams.Length; i++)
                {
                    SqlParameter value = new SqlParameter(myParams[i], myValues[i]);
                    sqlCommand.Parameters.Add(value);
                }
            }
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
            try
            {
                this.Rowaffected = sqlDataAdapter.Fill(dataTable);
                if (this.AutoCommand)
                {
                    this.CommandType = CommandType.StoredProcedure;
                }
                return dataTable;
            }
            catch (InvalidOperationException ex)
            {
                this.Close(myConnection);
                this.Result = (SqlHelper._HideError ? ("Không thể được lấy dữ liệu.\nChi tiết:\n\t" + ex.Message) : ex.Message);
                this.RaiseErrorEventHander(new SqlHelperException(this.Result));
            }
            catch (SqlException ex2)
            {
                this.Close(myConnection);
                if (SqlHelper._HideError)
                {
                    this.Result = this.DispError(ex2.Number, "Không thể được lấy dữ liệu.\nChi tiết:\n\t" + ex2.Message, commandText);
                }
                else
                {
                    this.Result = ex2.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result, ex2.Number));
            }
            catch (Exception ex3)
            {
                this.Close(myConnection);
                this.Result = (SqlHelper._HideError ? ("Không thể được lấy dữ liệu.\nChi tiết:\n\t" + ex3.Message) : ex3.Message);
                this.RaiseErrorEventHander(new SqlHelperException(this.Result));
            }
            return dataTable;
        }
        private void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, out bool mustCloseConnection)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }
            if (commandText == null || commandText.Length == 0)
            {
                throw new ArgumentNullException("commandText");
            }
            if (connection.State != ConnectionState.Open)
            {
                mustCloseConnection = true;
                connection.Open();
            }
            else
            {
                mustCloseConnection = false;
            }
            command.Connection = connection;
            command.CommandText = commandText;
            if (transaction != null)
            {
                if (transaction.Connection == null)
                {
                    throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
                }
                command.Transaction = transaction;
            }
            command.CommandType = commandType;
            if (commandParameters != null)
            {
                SqlHelper.AttachParameters(command, commandParameters);
            }
        }
        private void AssignParameterValues(SqlParameter[] commandParameters, object[] parameterValues)
        {
            if (commandParameters == null || parameterValues == null)
            {
                return;
            }
            if (commandParameters.Length != parameterValues.Length)
            {
                throw new ArgumentException("Parameter count does not match Parameter Value count.");
            }
            int i = 0;
            int num = commandParameters.Length;
            while (i < num)
            {
                if (parameterValues[i] is IDbDataParameter)
                {
                    IDbDataParameter dbDataParameter = (IDbDataParameter)parameterValues[i];
                    if (dbDataParameter.Value == null)
                    {
                        commandParameters[i].Value = DBNull.Value;
                    }
                    else
                    {
                        commandParameters[i].Value = dbDataParameter.Value;
                    }
                }
                else
                {
                    if (parameterValues[i] == null)
                    {
                        commandParameters[i].Value = DBNull.Value;
                    }
                    else
                    {
                        commandParameters[i].Value = parameterValues[i];
                    }
                }
                i++;
            }
        }
        private static void AttachParameters(SqlCommand command, SqlParameter[] commandParameters)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }
            if (commandParameters != null)
            {
                for (int i = 0; i < commandParameters.Length; i++)
                {
                    SqlParameter sqlParameter = commandParameters[i];
                    if (sqlParameter != null)
                    {
                        if ((sqlParameter.Direction == ParameterDirection.InputOutput || sqlParameter.Direction == ParameterDirection.Input) && sqlParameter.Value == null)
                        {
                            sqlParameter.Value = DBNull.Value;
                        }
                        command.Parameters.Add(sqlParameter);
                    }
                }
            }
        }
        public DataTable ExecuteDataTable(SqlConnection myConnection, string commandText)
        {
            return this.ExecuteDataTable(myConnection, commandText, null, null);
        }
        public DataTable ExecuteDataTable(SqlTransaction myTransaction, string commandText, string[] myParams, object[] myValues)
        {
            return this.ExecuteDataTable(myTransaction, this.CommandType, commandText, myParams, myValues);
        }
        public DataTable ExecuteDataTable(SqlTransaction myTransaction, CommandType commandType, string commandText, string[] myParams, object[] myValues)
        {
            if (myTransaction == null)
            {
                throw new SqlHelperException("myTransaction");
            }
            if (string.IsNullOrEmpty(commandText))
            {
                throw new ArgumentNullException("commandText");
            }
            if (myTransaction != null && myTransaction.Connection == null)
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            bool flag = false;
            if (myParams != null & myValues != null)
            {
                flag = true;
                if (myParams.Length != myValues.Length)
                {
                    throw new ArgumentException("Parameter count does not match Parameter Value count.");
                }
            }
            this.Result = "";
            DataTable dataTable = new DataTable();
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = commandText;
            sqlCommand.Connection = myTransaction.Connection;
            sqlCommand.Transaction = myTransaction;
            sqlCommand.CommandType = commandType;
            if (flag)
            {
                for (int i = 0; i < myParams.Length; i++)
                {
                    SqlParameter value = new SqlParameter(myParams[i], myValues[i]);
                    sqlCommand.Parameters.Add(value);
                }
            }
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
            try
            {
                this.Rowaffected = sqlDataAdapter.Fill(dataTable);
                if (this.AutoCommand)
                {
                    this.CommandType = CommandType.StoredProcedure;
                }
                return dataTable;
            }
            catch (InvalidOperationException ex)
            {
                this.RollBack(myTransaction);
                this.Result = (SqlHelper._HideError ? ("Không thể được lấy dữ liệu.\nChi tiết:\n\t" + ex.Message) : ex.Message);
                this.RaiseErrorEventHander(new SqlHelperException(this.Result));
            }
            catch (SqlException ex2)
            {
                this.RollBack(myTransaction);
                if (SqlHelper._HideError)
                {
                    this.Result = this.DispError(ex2.Number, "Không thể được lấy dữ liệu.\nChi tiết:\n\t" + ex2.Message, commandText);
                }
                else
                {
                    this.Result = ex2.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result, ex2.Number));
            }
            catch (Exception ex3)
            {
                this.RollBack(myTransaction);
                this.Result = (SqlHelper._HideError ? ("Không thể được lấy dữ liệu.\nChi tiết:\n\t" + ex3.Message) : ex3.Message);
                this.RaiseErrorEventHander(new SqlHelperException(this.Result));
            }
            return dataTable;
        }
        public DataTable ExecuteDataTable(SqlTransaction myTransaction, string commandText)
        {
            return this.ExecuteDataTable(myTransaction, commandText, null, null);
        }
        public DataSet ExecuteDataSet(string commandText, string[] myParams, object[] myValues)
        {
            DataSet result = null;
            if (this.m_useTransaction)
            {
                this.Result = this.Start();
                if (this.Result != "OK")
                {
                    this.RollBack();
                    return result;
                }
                result = this.ExecuteDataSet(this._transaction, commandText, myParams, myValues);
                if (this.Result == "OK")
                {
                    this.Commit();
                }
            }
            else
            {
                if (this.Open() == "OK")
                {
                    result = this.ExecuteDataSet(this._connection, commandText, myParams, myValues);
                }
                this.Close();
            }
            return result;
        }
        public DataSet ExecuteDataSet(string commandText)
        {
            return this.ExecuteDataSet(commandText, null, null);
        }
        public DataSet ExecuteDataSet(SqlConnection myConnection, string commandText, string[] myParams, object[] myValues)
        {
            return this.ExecuteDataSet(myConnection, this.CommandType, commandText, myParams, myValues);
        }
        public DataSet ExecuteDataSet(SqlConnection myConnection, CommandType commandType, string commandText, string[] myParams, object[] myValues)
        {
            if (myConnection == null)
            {
                throw new SqlHelperException("myConnection");
            }
            if (myConnection.State != ConnectionState.Open)
            {
                throw new SqlHelperException("Connnection is close");
            }
            if (string.IsNullOrEmpty(commandText))
            {
                throw new SqlHelperException("commandText");
            }
            bool flag = false;
            if (myParams != null & myValues != null)
            {
                flag = true;
                if (myParams.Length != myValues.Length)
                {
                    throw new SqlHelperException("Parameter count does not match Parameter Value count.");
                }
            }
            this.Result = "";
            DataSet dataSet = new DataSet();
            SqlCommand sqlCommand = myConnection.CreateCommand();
            sqlCommand.CommandText = commandText;
            sqlCommand.Connection = myConnection;
            sqlCommand.CommandType = commandType;
            if (flag)
            {
                for (int i = 0; i < myParams.Length; i++)
                {
                    SqlParameter value = new SqlParameter(myParams[i], myValues[i]);
                    sqlCommand.Parameters.Add(value);
                }
            }
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
            try
            {
                this.Rowaffected = sqlDataAdapter.Fill(dataSet);
                if (this.AutoCommand)
                {
                    this.CommandType = CommandType.StoredProcedure;
                }
                return dataSet;
            }
            catch (SqlException ex)
            {
                this.Close(myConnection);
                if (SqlHelper._HideError)
                {
                    this.Result = this.DispError(ex.Number, "Không thể được lấy dữ liệu.\nChi tiết:\n\t" + ex.Message, commandText);
                }
                else
                {
                    this.Result = ex.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result, ex.Number));
            }
            catch (Exception ex2)
            {
                this.Close(myConnection);
                this.Result = (SqlHelper._HideError ? ("Không thể được lấy dữ liệu.\nChi tiết:\n\t" + ex2.Message) : ex2.Message);
                this.RaiseErrorEventHander(new SqlHelperException(this.Result));
            }
            return dataSet;
        }
        public DataSet ExecuteDataSet(SqlConnection myConnection, string commandText)
        {
            return this.ExecuteDataSet(myConnection, commandText, null, null);
        }
        public DataSet ExecuteDataSet(SqlTransaction myTransaction, string commandText, string[] myParams, object[] myValues)
        {
            return this.ExecuteDataSet(myTransaction, this.CommandType, commandText, myParams, myValues);
        }
        public DataSet ExecuteDataSet(SqlTransaction myTransaction, CommandType commandType, string commandText, string[] myParams, object[] myValues)
        {
            if (myTransaction == null)
            {
                throw new SqlHelperException("myTransaction");
            }
            if (string.IsNullOrEmpty(commandText))
            {
                throw new SqlHelperException("commandText");
            }
            if (myTransaction.Connection == null)
            {
                throw new SqlHelperException("The transaction was rollbacked or commited, please provide an open transaction.");
            }
            bool flag = false;
            if (myParams != null & myValues != null)
            {
                flag = true;
                if (myParams.Length != myValues.Length)
                {
                    throw new SqlHelperException("Parameter count does not match Parameter Value count.");
                }
            }
            this.Result = "";
            DataSet dataSet = new DataSet();
            SqlCommand sqlCommand = myTransaction.Connection.CreateCommand();
            sqlCommand.CommandText = commandText;
            sqlCommand.Connection = myTransaction.Connection;
            sqlCommand.Transaction = myTransaction;
            sqlCommand.CommandType = commandType;
            if (flag)
            {
                for (int i = 0; i < myParams.Length; i++)
                {
                    SqlParameter value = new SqlParameter(myParams[i], myValues[i]);
                    sqlCommand.Parameters.Add(value);
                }
            }
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
            try
            {
                this.Rowaffected = sqlDataAdapter.Fill(dataSet);
                if (this.AutoCommand)
                {
                    this.CommandType = CommandType.StoredProcedure;
                }
                return dataSet;
            }
            catch (SqlException ex)
            {
                this.RollBack(myTransaction);
                if (SqlHelper._HideError)
                {
                    this.Result = this.DispError(ex.Number, "Không thể được lấy dữ liệu.\nChi tiết:\n\t" + ex.Message, commandText);
                }
                else
                {
                    this.Result = ex.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result, ex.Number));
            }
            catch (Exception ex2)
            {
                this.RollBack(myTransaction);
                this.Result = (SqlHelper._HideError ? ("Không thể được lấy dữ liệu.\nChi tiết:\n\t" + ex2.Message) : ex2.Message);
                this.RaiseErrorEventHander(new SqlHelperException(this.Result));
            }
            return dataSet;
        }
        public DataSet ExecuteDataSet(SqlTransaction myTransaction, string commandText)
        {
            return this.ExecuteDataSet(myTransaction, commandText, null, null);
        }
        public object ExecuteScalar(string commandText)
        {
            return this.ExecuteScalar(commandText, null, null);
        }
        public int ExecuteScalar(string commandText, int defautValue)
        {
            object obj = this.ExecuteScalar(commandText);
            if (obj != null)
            {
                return Convert.ToInt32(obj);
            }
            return defautValue;
        }
        public double ExecuteScalar(string commandText, double defautValue)
        {
            object obj = this.ExecuteScalar(commandText);
            if (obj != null)
            {
                return Convert.ToDouble(obj);
            }
            return defautValue;
        }
        public decimal ExecuteScalar(string commandText, decimal defautValue)
        {
            object obj = this.ExecuteScalar(commandText);
            if (obj != null)
            {
                return Convert.ToDecimal(obj);
            }
            return defautValue;
        }
        public string ExecuteScalar(string commandText, string defautValue)
        {
            object obj = this.ExecuteScalar(commandText);
            if (obj != null)
            {
                return Convert.ToString(obj);
            }
            return defautValue;
        }
        public object ExecuteScalar(SqlTransaction myTransaction, string commandText)
        {
            return this.ExecuteScalar(myTransaction, commandText, null, null);
        }
        public int ExecuteScalar(SqlTransaction myTransaction, string commandText, int defautValue)
        {
            return this.ExecuteScalar(myTransaction, commandText, null, null, defautValue);
        }
        public long ExecuteScalar2(SqlTransaction myTransaction, string commandText, int defautValue)
        {
            return this.ExecuteScalar2(myTransaction, commandText, null, null, defautValue);
        }
        public double ExecuteScalar(SqlTransaction myTransaction, string commandText, double defautValue)
        {
            return this.ExecuteScalar(myTransaction, commandText, null, null, defautValue);
        }
        public string ExecuteScalar(SqlTransaction myTransaction, string commandText, string defautValue)
        {
            return this.ExecuteScalar(myTransaction, commandText, null, null, defautValue);
        }
        public object ExecuteScalar(SqlConnection myConnection, string commandText)
        {
            return this.ExecuteScalar(myConnection, commandText, null, null);
        }
        public int ExecuteScalar(SqlConnection myConnection, string commandText, int defaultValue)
        {
            object obj = this.ExecuteScalar(myConnection, commandText);
            if (obj != null)
            {
                return Convert.ToInt32(obj);
            }
            return defaultValue;
        }
        public double ExecuteScalar(SqlConnection myConnection, string commandText, double defaultValue)
        {
            object obj = this.ExecuteScalar(myConnection, commandText);
            if (obj != null)
            {
                return Convert.ToDouble(obj);
            }
            return defaultValue;
        }
        public string ExecuteScalar(SqlConnection myConnection, string commandText, string defaultValue)
        {
            object obj = this.ExecuteScalar(myConnection, commandText);
            if (obj != null)
            {
                return Convert.ToString(obj);
            }
            return defaultValue;
        }
        public object ExecuteScalar(string commandText, string[] myParams, object[] myValues)
        {
            object result = null;
            if (this.m_useTransaction)
            {
                this.Result = this.Start();
                if (this.Result != "OK")
                {
                    this.RollBack();
                    return result;
                }
                result = this.ExecuteScalar(this._transaction, commandText, myParams, myValues);
                if (this.Result == "OK")
                {
                    this.Commit();
                }
            }
            else
            {
                if (this.Open() == "OK")
                {
                    result = this.ExecuteScalar(this._connection, commandText, myParams, myValues);
                }
                this.Close();
            }
            return result;
        }
        public long ExecuteScalar2(string commandText, string[] myParams, object[] myValues, int defaultValue)
        {
            object obj = this.ExecuteScalar(commandText, myParams, myValues);
            if (obj != null)
            {
                return Convert.ToInt64(obj);
            }
            return (long)defaultValue;
        }
        public int ExecuteScalar(string commandText, string[] myParams, object[] myValues, int defaultValue)
        {
            object obj = this.ExecuteScalar(commandText, myParams, myValues);
            if (obj != null)
            {
                return Convert.ToInt32(obj);
            }
            return defaultValue;
        }
        public double ExecuteScalar(string commandText, string[] myParams, object[] myValues, double defaultValue)
        {
            object obj = this.ExecuteScalar(commandText, myParams, myValues);
            if (obj != null)
            {
                return Convert.ToDouble(obj);
            }
            return defaultValue;
        }
        public decimal ExecuteScalar(string commandText, string[] myParams, object[] myValues, decimal defaultValue)
        {
            object obj = this.ExecuteScalar(commandText, myParams, myValues);
            if (obj != null)
            {
                return Convert.ToDecimal(obj);
            }
            return defaultValue;
        }
        public string ExecuteScalar(string commandText, string[] myParams, object[] myValues, string defaultValue)
        {
            object obj = this.ExecuteScalar(commandText, myParams, myValues);
            if (obj != null)
            {
                return Convert.ToString(obj);
            }
            return defaultValue;
        }
        public object ExecuteScalar(SqlConnection myConnection, string commandText, string[] myParams, object[] myValues)
        {
            return this.ExecuteScalar(myConnection, this.CommandType, commandText, myParams, myValues);
        }
        private object ExecuteScalar(SqlConnection myConnection, CommandType commandType, string commandText, string[] myParams, object[] myValues)
        {
            if (myConnection == null)
            {
                throw new SqlHelperException("myConnection");
            }
            if (string.IsNullOrEmpty(commandText))
            {
                throw new SqlHelperException("commandText");
            }
            if (myConnection.State != ConnectionState.Open)
            {
                throw new SqlHelperException("Connnection is close");
            }
            bool flag = false;
            if (myParams != null & myValues != null)
            {
                flag = true;
                if (myParams.Length != myValues.Length)
                {
                    throw new SqlHelperException("Parameter count does not match Parameter Value count.");
                }
            }
            object obj = null;
            this.Result = "";
            SqlCommand sqlCommand = myConnection.CreateCommand();
            sqlCommand.CommandText = commandText;
            sqlCommand.Connection = myConnection;
            sqlCommand.CommandType = commandType;
            if (flag)
            {
                for (int i = 0; i < myParams.Length; i++)
                {
                    SqlParameter value = new SqlParameter(myParams[i], myValues[i]);
                    sqlCommand.Parameters.Add(value);
                }
            }
            try
            {
                obj = sqlCommand.ExecuteScalar();
                if (obj != null && obj == DBNull.Value)
                {
                    obj = null;
                }
                if (this.AutoCommand)
                {
                    this.CommandType = CommandType.StoredProcedure;
                }
                return obj;
            }
            catch (SqlException ex)
            {
                this.Close(myConnection);
                if (SqlHelper._HideError)
                {
                    this.Result = "Không lấy được dữ liệu.\nChi tiết:\n\t" + ex.Message;
                    this.Result = this.DispError(ex.Number, this.Result, commandText);
                }
                else
                {
                    this.Result = ex.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result, ex.Number));
            }
            catch (Exception ex2)
            {
                this.Close(myConnection);
                if (SqlHelper._HideError)
                {
                    this.Result = "Không lấy được dữ liệu.\nChi tiết:\n\t" + ex2.Message;
                }
                else
                {
                    this.Result = ex2.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result));
            }
            return obj;
        }
        public int ExecuteScalar(SqlConnection myConnection, string commandText, string[] myParams, object[] myValues, int defaultValue)
        {
            object obj = this.ExecuteScalar(myConnection, commandText, myParams, myValues);
            if (obj != null)
            {
                return Convert.ToInt32(obj);
            }
            return defaultValue;
        }
        public double ExecuteScalar(SqlConnection myConnection, string commandText, string[] myParams, object[] myValues, double defaultValue)
        {
            object obj = this.ExecuteScalar(myConnection, commandText, myParams, myValues);
            if (obj != null)
            {
                return Convert.ToDouble(obj);
            }
            return defaultValue;
        }
        public decimal ExecuteScalar(SqlConnection myConnection, string commandText, string[] myParams, object[] myValues, decimal defaultValue)
        {
            object obj = this.ExecuteScalar(myConnection, commandText, myParams, myValues);
            if (obj != null)
            {
                return Convert.ToDecimal(obj);
            }
            return defaultValue;
        }
        public string ExecuteScalar(SqlConnection myConnection, string commandText, string[] myParams, object[] myValues, string defaultValue)
        {
            object obj = this.ExecuteScalar(myConnection, commandText, myParams, myValues);
            if (obj != null)
            {
                return Convert.ToString(obj);
            }
            return defaultValue;
        }
        public object ExecuteScalar(SqlTransaction myTransaction, string commandText, string[] myParams, object[] myValues)
        {
            return this.ExecuteScalar(myTransaction, this.CommandType, commandText, myParams, myValues);
        }
        public object ExecuteScalar(SqlTransaction myTransaction, CommandType commandType, string commandText, string[] myParams, object[] myValues)
        {
            if (myTransaction == null)
            {
                throw new SqlHelperException("myTransaction");
            }
            if (commandText == null)
            {
                throw new SqlHelperException("commandText");
            }
            if (myTransaction != null && myTransaction.Connection == null)
            {
                throw new SqlHelperException("The transaction was rollbacked or commited, please provide an open transaction.");
            }
            bool flag = false;
            if (myParams != null & myValues != null)
            {
                flag = true;
                if (myParams.Length != myValues.Length)
                {
                    throw new ArgumentException("Parameter count does not match Parameter Value count.");
                }
            }
            this.Result = "";
            object obj = null;
            SqlCommand sqlCommand = myTransaction.Connection.CreateCommand();
            sqlCommand.CommandText = commandText;
            sqlCommand.Connection = myTransaction.Connection;
            sqlCommand.Transaction = myTransaction;
            sqlCommand.CommandType = commandType;
            if (flag)
            {
                for (int i = 0; i < myParams.Length; i++)
                {
                    SqlParameter value = new SqlParameter(myParams[i], myValues[i]);
                    sqlCommand.Parameters.Add(value);
                }
            }
            try
            {
                obj = sqlCommand.ExecuteScalar();
                if (obj != null && obj == DBNull.Value)
                {
                    obj = null;
                }
                if (this.AutoCommand)
                {
                    this.CommandType = CommandType.StoredProcedure;
                }
                return obj;
            }
            catch (SqlException ex)
            {
                this.RollBack(myTransaction);
                if (SqlHelper._HideError)
                {
                    this.Result = "Không lấy được dữ liệu.\nChi tiết:\n\t" + ex.Message;
                    this.Result = this.DispError(ex.Number, this.Result, commandText);
                }
                else
                {
                    this.Result = ex.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result, ex.Number));
            }
            catch (Exception ex2)
            {
                this.RollBack(myTransaction);
                if (SqlHelper._HideError)
                {
                    this.Result = "Không lấy được dữ liệu.\nChi tiết:\n\t" + ex2.Message;
                }
                else
                {
                    this.Result = ex2.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result));
            }
            return obj;
        }
        public int ExecuteScalar(SqlTransaction myTraTransaction, string commandText, string[] myParams, object[] myValues, int defaultValue)
        {
            object obj = this.ExecuteScalar(myTraTransaction, commandText, myParams, myValues);
            if (obj != null)
            {
                return Convert.ToInt32(obj);
            }
            return defaultValue;
        }
        public long ExecuteScalar2(SqlTransaction myTraTransaction, string commandText, string[] myParams, object[] myValues, int defaultValue)
        {
            object obj = this.ExecuteScalar(myTraTransaction, commandText, myParams, myValues);
            if (obj != null)
            {
                return Convert.ToInt64(obj);
            }
            return (long)defaultValue;
        }
        public double ExecuteScalar(SqlTransaction myTraTransaction, string commandText, string[] myParams, object[] myValues, double defaultValue)
        {
            object obj = this.ExecuteScalar(myTraTransaction, commandText, myParams, myValues);
            if (obj != null)
            {
                return Convert.ToDouble(obj);
            }
            return defaultValue;
        }
        public decimal ExecuteScalar(SqlTransaction myTraTransaction, string commandText, string[] myParams, object[] myValues, decimal defaultValue)
        {
            object obj = this.ExecuteScalar(myTraTransaction, commandText, myParams, myValues);
            if (obj != null)
            {
                return Convert.ToDecimal(obj);
            }
            return defaultValue;
        }
        public string ExecuteScalar(SqlTransaction myTraTransaction, string commandText, string[] myParams, object[] myValues, string defaultValue)
        {
            object obj = this.ExecuteScalar(myTraTransaction, commandText, myParams, myValues);
            if (obj != null)
            {
                return Convert.ToString(obj);
            }
            return defaultValue;
        }

        public SqlDataReader ExecuteReader(string commandText)
        {
            return this.ExecuteReader(commandText, null, null);
        }
        public SqlDataReader ExecuteReader(SqlConnection myConnection, string commandText)
        {
            return this.ExecuteReader(myConnection, commandText, null, null);
        }
        public SqlDataReader ExecuteReader(SqlTransaction myTransaction, string commandText)
        {
            return this.ExecuteReader(myTransaction, commandText, null, null);
        }
        public SqlDataReader ExecuteReader(string commandText, string[] myParams, object[] myValues)
        {
            SqlDataReader result = null;
            if (this.m_useTransaction)
            {
                this.Result = this.Start();
                if (this.Result != "OK")
                {
                    this.RollBack();
                    return result;
                }
                result = this.ExecuteReader(this._transaction, commandText, myParams, myValues);
                if (this.Result == "OK")
                {
                    this.Commit();
                }
            }
            else
            {
                if (this.Open() == "OK")
                {
                    result = this.ExecuteReader(this._connection, commandText, myParams, myValues);
                }
            }
            return result;
        }
        public SqlDataReader ExecuteReader(SqlConnection myConnection, string commandText, string[] myParams, object[] myValues)
        {
            return this.ExecuteReader(myConnection, this.CommandType, commandText, myParams, myValues);
        }
        public SqlDataReader ExecuteReader(SqlConnection myConnection, CommandType commandType, string commandText, string[] myParams, object[] myValues)
        {
            if (myConnection == null)
            {
                throw new SqlHelperException("myConnection");
            }
            if (myConnection.State != ConnectionState.Open)
            {
                throw new SqlHelperException("Connnection is close");
            }
            if (string.IsNullOrEmpty(commandText))
            {
                throw new SqlHelperException("commandText");
            }
            bool flag = false;
            if (myParams != null & myValues != null)
            {
                flag = true;
                if (myParams.Length != myValues.Length)
                {
                    throw new SqlHelperException("Parameter count does not match Parameter Value count.");
                }
            }
            SqlCommand sqlCommand = myConnection.CreateCommand();
            sqlCommand.CommandText = commandText;
            sqlCommand.Connection = myConnection;
            sqlCommand.CommandType = commandType;
            if (flag)
            {
                for (int i = 0; i < myParams.Length; i++)
                {
                    SqlParameter value = new SqlParameter(myParams[i], myValues[i]);
                    sqlCommand.Parameters.Add(value);
                }
            }
            try
            {
                SqlDataReader result = sqlCommand.ExecuteReader();
                if (this.AutoCommand)
                {
                    this.CommandType = CommandType.StoredProcedure;
                }
                return result;
            }
            catch (SqlException ex)
            {
                this.Close(myConnection);
                if (SqlHelper._HideError)
                {
                    this.Result = this.DispError(ex.Number, "Không lấy được dữ liệu.\nChi tiết:\n\t" + ex.Message, commandText);
                }
                else
                {
                    this.Result = ex.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result, ex.Number));
            }
            catch (Exception ex2)
            {
                this.Close(myConnection);
                if (SqlHelper._HideError)
                {
                    this.Result = "Không lấy được dữ liệu.\nChi tiết:\n\t" + ex2.Message;
                }
                else
                {
                    this.Result = ex2.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result));
            }
            return null;
        }
        public SqlDataReader ExecuteReader(SqlTransaction myTransaction, string commandText, string[] myParams, object[] myValues)
        {
            return this.ExecuteReader(myTransaction, this.CommandType, commandText, myParams, myValues);
        }
        public SqlDataReader ExecuteReader(SqlTransaction myTransaction, CommandType commandType, string commandText, string[] myParams, object[] myValues)
        {
            if (myTransaction == null)
            {
                throw new SqlHelperException("myTransaction");
            }
            if (commandText == null)
            {
                throw new SqlHelperException("commandText");
            }
            if (myTransaction != null && myTransaction.Connection == null)
            {
                throw new SqlHelperException("The transaction was rollbacked or commited, please provide an open transaction.");
            }
            bool flag = false;
            if (myParams != null & myValues != null)
            {
                flag = true;
                if (myParams.Length != myValues.Length)
                {
                    throw new ArgumentException("Parameter count does not match Parameter Value count.");
                }
            }
            SqlCommand sqlCommand = myTransaction.Connection.CreateCommand();
            sqlCommand.CommandText = commandText;
            sqlCommand.Connection = myTransaction.Connection;
            sqlCommand.Transaction = myTransaction;
            sqlCommand.CommandType = commandType;
            if (flag)
            {
                for (int i = 0; i < myParams.Length; i++)
                {
                    SqlParameter value = new SqlParameter(myParams[i], myValues[i]);
                    sqlCommand.Parameters.Add(value);
                }
            }
            try
            {
                SqlDataReader result = sqlCommand.ExecuteReader();
                if (this.AutoCommand)
                {
                    this.CommandType = CommandType.StoredProcedure;
                }
                return result;
            }
            catch (SqlException ex)
            {
                this.RollBack(myTransaction);
                if (SqlHelper._HideError)
                {
                    this.Result = this.DispError(ex.Number, "Không lấy được dữ liệu.\nChi tiết:\n\t" + ex.Message, commandText);
                }
                else
                {
                    this.Result = ex.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result, ex.Number));
            }
            catch (Exception ex2)
            {
                this.RollBack(myTransaction);
                if (SqlHelper._HideError)
                {
                    this.Result = "Không lấy được dữ liệu.\nChi tiết:\n\t" + ex2.Message;
                }
                else
                {
                    this.Result = ex2.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result));
            }
            return null;
        }
        public string ExecuteNonQuery(string commandText)
        {
            return this.ExecuteNonQuery(commandText, null, null);
        }
        public string ExecuteNonQuery(string commandText, string[] myParams, object[] myValues)
        {
            if (this.m_useTransaction)
            {
                this.Result = this.Start();
                if (this.Result != "OK")
                {
                    this.RollBack();
                    return this.Result;
                }
                this.Result = this.ExecuteNonQuery(this._transaction, commandText, myParams, myValues);
                if (this.Result == "OK")
                {
                    this.Result = this.Commit();
                }
            }
            else
            {
                this.Result = this.Open();
                if (this.Result == "OK")
                {
                    this.Result = this.ExecuteNonQuery(this._connection, commandText, myParams, myValues);
                }
                this.Close();
            }
            return this.Result;
        }
        public string ExecuteNonQuery(string[] commandTexts)
        {
            this.Result = "Không thực hiện được";
            if (this.Open() == "OK")
            {
                this.Result = this.Start();
                if (this.Result != "OK")
                {
                    this.RollBack();
                    return this.Result;
                }
                for (int i = 0; i < commandTexts.Length; i++)
                {
                    this.Result = this.ExecuteNonQuery(this._transaction, commandTexts[i]);
                    if (this.Result != "OK")
                    {
                        this.RollBack();
                        return this.Result;
                    }
                }
                this.Commit();
            }
            return this.Result;
        }
        public string ExecuteNonQuery(SqlTransaction myTransaction, string[] commandTexts)
        {
            string text = "Không thực hiện được";
            for (int i = 0; i < commandTexts.Length; i++)
            {
                text = this.ExecuteNonQuery(myTransaction, commandTexts[i]);
                if (text != "OK")
                {
                    this.RollBack(myTransaction);
                    return text;
                }
            }
            return text;
        }
        public string ExecuteNonQuery(SqlTransaction myTransaction, CommandType commandType, StringCollection commandTexts)
        {
            string text = "OK";
            for (int i = 0; i < commandTexts.Count; i++)
            {
                this.CommandType = commandType;
                text = commandTexts[i];
                if (!(text == ""))
                {
                    text = this.ExecuteNonQuery(myTransaction, text);
                    if (text != "OK")
                    {
                        this.RollBack(myTransaction);
                        return text;
                    }
                }
            }
            return text;
        }
        public string ExecuteNonQuery(StringCollection commandTexts)
        {
            return this.ExecuteNonQuery(commandTexts, false);
        }
        public string ExecuteNonQuery(StringCollection commandTexts, bool isTransaction)
        {
            this.Result = "Không thực hiện được";
            if (commandTexts == null)
            {
                throw new SqlHelperException("Null object", 0);
            }
            if (commandTexts.Count == 0)
            {
                return "OK";
            }
            if (this.Open() == "OK")
            {
                if (isTransaction)
                {
                    this.Result = this.Start();
                    if (this.Result != "OK")
                    {
                        this.RollBack();
                        return this.Result;
                    }
                    for (int i = 0; i < commandTexts.Count; i++)
                    {
                        this.Result = this.ExecuteNonQuery(this._transaction, commandTexts[i]);
                        if (this.Result != "OK")
                        {
                            this.RollBack();
                            return this.Result;
                        }
                    }
                    this.Commit();
                }
                else
                {
                    for (int j = 0; j < commandTexts.Count; j++)
                    {
                        this.Result = this.ExecuteNonQuery(this._connection, commandTexts[j]);
                        if (this.Result != "OK")
                        {
                            return this.Result;
                        }
                    }
                }
            }
            return this.Result;
        }
        public string ExecuteNonQuery(SqlConnection myConnection, string commandText, string[] myParams, object[] myValues)
        {
            return this.ExecuteNonQuery(myConnection, this.CommandType, commandText, myParams, myValues);
        }
        public string ExecuteNonQuery(SqlConnection myConnection, CommandType commandType, string commandText, string[] myParams, object[] myValues)
        {
            this.Result = "";
            if (myConnection == null)
            {
                throw new SqlHelperException("Connection is null");
            }
            if (string.IsNullOrEmpty(commandText))
            {
                throw new SqlHelperException("commandText");
            }
            if (myConnection.State != ConnectionState.Open)
            {
                throw new SqlHelperException("Connnection is close");
            }
            bool flag = false;
            if (myParams != null & myValues != null)
            {
                flag = true;
                if (myParams.Length != myValues.Length)
                {
                    throw new SqlHelperException("Parameter count does not match Parameter Value count.");
                }
            }
            SqlCommand sqlCommand = myConnection.CreateCommand();
            sqlCommand.CommandText = commandText;
            sqlCommand.Connection = myConnection;
            sqlCommand.CommandType = commandType;
            if (flag)
            {
                for (int i = 0; i < myParams.Length; i++)
                {
                    SqlParameter value = new SqlParameter(myParams[i], myValues[i]);
                    sqlCommand.Parameters.Add(value);
                }
            }
            try
            {
                this.Rowaffected = sqlCommand.ExecuteNonQuery();
                this.Result = "OK";
                if (this.AutoCommand)
                {
                    this.CommandType = CommandType.StoredProcedure;
                }
                return this.Result;
            }
            catch (SqlException ex)
            {
                this.Close(myConnection);
                if (SqlHelper._HideError)
                {
                    this.Result = this.DispError(ex.Number, "Không thể thực hiện tác vụ này.\nChi tiết:\n\t" + ex.Message, commandText);
                }
                else
                {
                    this.Result = ex.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result, ex.Number));
            }
            catch (Exception ex2)
            {
                this.Close(myConnection);
                if (SqlHelper._HideError)
                {
                    this.Result = "Không thể thực hiện tác vụ này.\nChi tiết:\n\t" + ex2.Message;
                }
                else
                {
                    this.Result = ex2.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result));
            }
            return this.Result;
        }
        public string ExecuteNonQuery(string connectionString, string commandText, string[] myParams, object[] myValues)
        {
            if (connectionString == null)
            {
                throw new ArgumentNullException("connectionString");
            }
            this._connectionString = connectionString;
            return this.ExecuteNonQuery(commandText, myParams, myValues);
        }
        public string ExecuteNonQuery(SqlConnection myConnection, string commandText)
        {
            return this.ExecuteNonQuery(myConnection, commandText, null, null);
        }
        public string ExecuteNonQuery(SqlTransaction myTransaction, string commandText, string[] myParams, object[] myValues)
        {
            return this.ExecuteNonQuery(myTransaction, this.CommandType, commandText, myParams, myValues);
        }
        public string ExecuteNonQuery(SqlTransaction myTransaction, CommandType commandType, string commandText, string[] myParams, object[] myValues)
        {
            if (myTransaction == null)
            {
                throw new SqlHelperException("Transaction is null");
            }
            if (commandText == null)
            {
                throw new SqlHelperException("commandText");
            }
            if (myTransaction != null && myTransaction.Connection == null)
            {
                throw new SqlHelperException("The transaction was rollbacked or commited, please provide an open transaction.");
            }
            bool flag = false;
            if (myParams != null & myValues != null)
            {
                flag = true;
                if (myParams.Length != myValues.Length)
                {
                    throw new SqlHelperException("Parameter count does not match Parameter Value count.");
                }
            }
            SqlCommand sqlCommand = myTransaction.Connection.CreateCommand();
            sqlCommand.CommandText = commandText;
            sqlCommand.Connection = myTransaction.Connection;
            sqlCommand.Transaction = myTransaction;
            sqlCommand.CommandType = commandType;
            if (flag)
            {
                for (int i = 0; i < myParams.Length; i++)
                {
                    SqlParameter value = new SqlParameter(myParams[i], myValues[i]);
                    sqlCommand.Parameters.Add(value);
                }
            }
            try
            {
                this.Rowaffected = sqlCommand.ExecuteNonQuery();
                this.Result = "OK";
                if (this.AutoCommand)
                {
                    this.CommandType = CommandType.StoredProcedure;
                }
                return this.Result;
            }
            catch (SqlException ex)
            {
                this.RollBack(myTransaction);
                if (SqlHelper._HideError)
                {
                    this.Result = this.DispError(ex.Number, "Không thể thực hiện tác vụ này.\nChi tiết:\n\t" + ex.Message, commandText);
                }
                else
                {
                    this.Result = ex.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result, ex.Number));
            }
            catch (Exception ex2)
            {
                this.RollBack(myTransaction);
                if (SqlHelper._HideError)
                {
                    this.Result = "Không thể thực hiện tác vụ này.\nChi tiết:\n\t" + ex2.Message;
                }
                else
                {
                    this.Result = ex2.Message;
                }
                this.RaiseErrorEventHander(new SqlHelperException(this.Result));
            }
            return this.Result;
        }
        public string ExecuteNonQuery(SqlTransaction myTransaction, string commandText)
        {
            return this.ExecuteNonQuery(myTransaction, commandText, null, null);
        }
        public bool Check()
        {
            return this.Check(false);
        }
        public bool Check(bool isclose)
        {
            if (this.Open() == "OK")
            {
                if (isclose)
                {
                    this.Close();
                }
                return true;
            }
            return false;
        }
        public string Exists(string expression)
        {
            this.Result = "Cơ sở dữ liệu đã tồn tại.";
            string commandText = "DECLARE @return_value int if EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'" + expression + "') set @return_value =1 else set @return_value =0 select @return_value";
            this.CommandType = CommandType.Text;
            object obj = this.ExecuteScalar(commandText);
            if (obj != Convert.DBNull && Convert.ToInt32(obj) == 0)
            {
                this.Result = "OK";
            }
            return this.Result;
        }
        public string Attach(string mdfFile, string ldfFile, string database)
        {
            this.Result = this.Exists(database);
            if (this.Result == "OK")
            {
                string commandText = string.Concat(new string[]
				{
					"EXEC sp_attach_db @dbname = N'",
					database,
					"',@filename1 = N'",
					mdfFile,
					"',@filename2 = N'",
					ldfFile,
					"'"
				});
                this.CommandType = CommandType.Text;
                this.Result = this.ExecuteNonQuery(commandText);
            }
            return this.Result;
        }
        public string Deattach(string database)
        {
            this.Result = this.Open();
            if (this.Result == "OK")
            {
                string commandText = string.Concat(new string[]
				{
					"IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'",
					database,
					"') BEGIN  EXEC sp_detach_db @dbname = N'",
					database,
					"' END "
				});
                this.CommandType = CommandType.Text;
                this.Result = this.ExecuteNonQuery(commandText);
            }
            return this.Result;
        }
        public string AddUser(string userId, string password, string database)
        {
            this.CommandType = CommandType.Text;
            return this.ExecuteNonQuery(string.Concat(new string[]
			{
				"EXEC master.dbo.sp_addlogin @loginame = N'",
				userId,
				"', @passwd = N'",
				password,
				"', @defdb = N'",
				database,
				"'"
			}));
        }
        public string RebuildConnectionString()
        {
            return this.RebuildConnectionString(this._server, this._database, this._userid, this._password, this._authentication);
        }
        public string RebuildConnectionString(string server, string database, string user, string password, bool authentication)
        {
            if (!authentication)
            {
                if (database != "")
                {
                    return string.Concat(new string[]
					{
						"server=",
						server,
						";database=",
						database,
						";user id= ",
						user,
						" ;password=",
						password,
						";"
					});
                }
                return string.Concat(new string[]
				{
					"server=",
					server,
					";database=master;user id= ",
					user,
					" ;password=",
					password,
					";"
				});
            }
            else
            {
                if (database != "")
                {
                    return string.Concat(new string[]
					{
						"Data Source=",
						server,
						";Initial Catalog=",
						database,
						";Integrated Security=True;"
					});
                }
                return "Data Source=" + server + ";Initial Catalog=master;Integrated Security=True;";
            }
        }
        public string RebuildConnectionString(string server, string user, string password)
        {
            return this.RebuildConnectionString(server, "", user, password, false);
        }
        public string RebuildConnectionString(string server)
        {
            return this.RebuildConnectionString(server, "", "", "", true);
        }
        public string RebuildConnectionString(string server, string database)
        {
            return this.RebuildConnectionString(server, database, "", "", true);
        }
        private string DispError(int number, string message, string function)
        {
            if (number <= 201)
            {
                if (number <= 2)
                {
                    if (number == -2)
                    {
                        return "Thời gian giao dịch quá lâu, vì an toàn kết nối đến tự động ngắt.";
                    }
                    if (number == 2)
                    {
                        return "Không thể kết nối với máy chủ Sql Server.";
                    }
                }
                else
                {
                    if (number == 58)
                    {
                        return "Không thể kết nối đến máy chủ,\ndo các nguyên nhân(máy chủ không tồn tại,\nphương thức kết nối không đúng,\nmáy chủ không cho phép truy cập từ xa).";
                    }
                    if (number == 201)
                    {
                        return "Thủ tục hoặc chức năng " + function.ToUpper() + " đối số sử dụng không đúng.";
                    }
                }
            }
            else
            {
                if (number <= 2812)
                {
                    if (number == 233)
                    {
                        return "Không thể thực hiện yêu cầu máy chủ mở giao dịch.";
                    }
                    if (number == 2812)
                    {
                        return "Không tìm thấy thủ tục " + function.ToUpper() + ".\nCó thể cở sở dữ liệu này đã quá cũ vui lòng cập nhật mới.";
                    }
                }
                else
                {
                    if (number == 4060)
                    {
                        return "Không thể mở kết nối đến cở sở dữ liệu, Có thể cở sở dữ liệu này không tồn tại.";
                    }
                    if (number == 8146)
                    {
                        return "Thủ tục " + function.ToUpper() + " không có đối số.";
                    }
                    if (number == 18456)
                    {
                        return "Không thể đăng nhập vào máy chủ Sql Server với tài khoản này.\nCó thể tài khoản hoặc mật khẩu không đúng.";
                    }
                }
            }
            if (message.Length > 0)
            {
                int num = message.IndexOf('\n');
                if (num != -1)
                {
                    message = message.Substring(0, num);
                }
            }
            return message;
        }
        public static bool IsNumeric(object expression)
        {
            double num;
            return double.TryParse(Convert.ToString(expression), NumberStyles.Any, NumberFormatInfo.InvariantInfo, out num);
        }
        public static string CheckId(string tableName, string columnName, string vKey)
        {
            string result = "";
            SqlHelper sqlHelper = new SqlHelper
            {
                CommandType = CommandType.Text
            };
            object obj = sqlHelper.ExecuteScalar(string.Concat(new string[]
			{
				"select count([",
				columnName,
				"]) from [",
				tableName,
				"] where [",
				columnName,
				"] like '",
				vKey,
				"'"
			}));
            if (obj != null && Convert.ToInt32(obj) == 0)
            {
                result = "OK";
            }
            return result;
        }
        public static string GenCode(string tableName, string columnName, string fKey)
        {
            object obj = new SqlHelper
            {
                CommandType = CommandType.Text
            }.ExecuteScalar(string.Concat(new string[]
			{
				"select max([",
				columnName,
				"]) from [",
				tableName,
				"] where [",
				columnName,
				"] like N'",
				fKey,
				"%'"
			}));
            string text = (obj == null) ? "0" : obj.ToString();
            if (fKey.Length != 0)
            {
                text = text.Replace(fKey, "").Trim();
            }
            int num = 0;
            if (SqlHelper.IsNumeric(text))
            {
                num = (int)Conversion.Val(text);
            }
            string text2 = (num + 1).ToString();
            while (text2.Length < 6)
            {
                text2 = "0" + text2;
            }
            return fKey + text2;
        }
        public static string GenCode(string fKey)
        {
            return SqlHelper.GenCode("TRANS_REF", "RefID", fKey);
        }
        public static string GenCode(string tableName, string columnName, string fKey, int NumberZero)
        {
            object obj = new SqlHelper
            {
                CommandType = CommandType.Text
            }.ExecuteScalar(string.Concat(new string[]
	        {
		        "select max([",
		        columnName,
		        "]) from [",
		        tableName,
		        "] where [",
		        columnName,
		        "] like N'",
		        fKey,
		        "%'"
	        }));
            string text = (obj == null) ? "0" : obj.ToString();
            if (fKey.Length != 0)
            {
                text = text.Replace(fKey, "").Trim();
            }
            int num = 0;
            if (SqlHelper.IsNumeric(text))
            {
                num = (int)Conversion.Val(text);
            }
            string text2 = (num + 1).ToString();
            while (text2.Length < NumberZero)
            {
                text2 = "0" + text2;
            }
            return fKey + text2;
        }
        public static string GenCode(SqlHelper mySql, string tableName, string columnName, string fKey)
        {
            return SqlHelper.GenCode(mySql.Transaction, tableName, columnName, fKey);
        }
        public static string GenCode(SqlTransaction myTransaction, string tableName, string columnName, string fKey)
        {
            if (myTransaction == null)
            {
                throw new ArgumentNullException("transaction");
            }
            if (myTransaction != null && myTransaction.Connection == null)
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            object obj = new SqlHelper
            {
                CommandType = CommandType.Text
            }.ExecuteScalar(myTransaction, string.Concat(new string[]
			{
				"select max(",
				columnName,
				") from [",
				tableName,
				"] where [",
				columnName,
				"] like N'",
				fKey,
				"%'"
			}));
            string text = (obj == null) ? "0" : obj.ToString();
            if (fKey.Length != 0)
            {
                text = text.Replace(fKey, "").Trim();
            }
            int num = 0;
            if (Information.IsNumeric(text))
            {
                num = (int)Conversion.Val(text);
            }
            string text2 = (num + 1).ToString();
            while (text2.Length < 6)
            {
                text2 = "0" + text2;
            }
            return fKey + text2;
        }
        public static string GenCode(SqlTransaction myTransaction, string fKey)
        {
            return SqlHelper.GenCode(myTransaction, "TRANS_REF", "RefID", fKey);
        }
        public void SaveMyConfig()
        {
            MyConfig myConfig = new MyConfig();
            string val;
            if (!this._authentication)
            {
                val = string.Concat(new string[]
				{
					"server=",
					this._server,
					";database=",
					this._database,
					";user id= ",
					this._userid,
					" ;password=",
					this._password,
					";"
				});
                myConfig.SetValue("//appSettings//add[@key='connectionString']", val);
                val = string.Concat(new string[]
				{
					"server=",
					this._server,
					";database=Master;user id= ",
					this._userid,
					" ;password=",
					this._password,
					";"
				});
                myConfig.SetValue("//appSettings//add[@key='ConnectionRestore']", val);
                return;
            }
            val = string.Concat(new string[]
			{
				"Data Source=",
				this._server,
				";Initial Catalog=",
				this._database,
				";Integrated Security=True;"
			});
            myConfig.SetValue("//appSettings//add[@key='connectionString']", val);
            val = "Data Source=" + this._server + ";Initial Catalog=Master;Integrated Security=True;";
            myConfig.SetValue("//appSettings//add[@key='ConnectionRestore']", val);
        }
    }
}